/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

#if WINDOWS_PHONE

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input.Touch;

namespace FrozenBubble
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class GestureController : Microsoft.Xna.Framework.GameComponent, IInputController
    {
        private InputState _currentState;

        private Rectangle _leftRectangle;
        private Rectangle _rightRectangle;
        private Rectangle _fireRectangle;

        public GestureController(Game game)
            : base(game)
        {
            _currentState = new InputState(false, false, false, false, false, false, false);

            _leftRectangle = new Rectangle(0, 500, 200, 300);
            _rightRectangle = new Rectangle(280, 500, 200, 300);
            _fireRectangle = new Rectangle(200, 600, 80, 200);

            // Enabled the specified gestures
            TouchPanel.EnabledGestures = GestureType.DoubleTap;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        public InputState State
        {
            get
            {
                return _currentState;
            }
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            bool left = false;
            bool right = false;
            bool fire = false;
            bool center = false;
            bool pause = false;
            bool any = false;
            bool end = (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed);

            MouseState state = Mouse.GetState();

            if (state.LeftButton == ButtonState.Pressed)
            {
                int x = state.X;
                int y = state.Y;

                if (_leftRectangle.Contains(x, y))
                {
                    left = true;
                }
                else if (_rightRectangle.Contains(x, y))
                {
                    right = true;
                }

                any = true;
            }

            // See if we have a double tag gesture
            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();
                if (gesture.GestureType == GestureType.DoubleTap)
                {
                    fire = true;
                }
            }

            _currentState = new InputState(left, right, fire, center, pause, any, end);

            base.Update(gameTime);
        }
    }
}

#endif